<?php
    $userID = $_COOKIE['user'];
    $libraryCardNo = $_GET['libraryCardNo'];
    $materialID = $_GET['materialID'];
    $reserve_date = date("Y-m-d H:i.s",time());
    $expire_date = date("Y-m-d H:i.s",time()+86400);
    //echo "$reserve_date";
    //echo "$expire_date";
    require('db_connection.php');
    $query8 = "SELECT * FROM tblcirculation WHERE userID=$userID and status LIKE 'reserved'";
    $result8 = mysql_query($query8);
    if(mysql_num_rows($result8) > 0)
    {
      echo"<script>alert('You have an existing reservation.')</script>";
      echo"<script>history.back()</script>";
      
    }
    else
    {
        reserve_material($materialID,$reserve_date,$expire_date);
    }
    
  //function to verify reservation
  function reserve_material ($materialID,$reserve_date,$expire_date)
  {
    $libraryCardNo = $_GET['libraryCardNo'];
    //echo"pwede 2";
    require('db_connection.php');
    //check for book details

    $query6 = "SELECT * FROM tblclient WHERE libraryCardNo like '$libraryCardNo'";
    $result6 = mysql_query($query6);
        while($row=mysql_fetch_array($result6))
        {
          $userID = $row['userID'];
        }
        $query7 = "SELECT * FROM tblcollection WHERE materialID like '$materialID'";
        $result7 = mysql_query($query7);
        while($row=mysql_fetch_array($result7))
        {
          $libraryNo = $row['libraryNo'];
        }
    
    $query3 = "INSERT INTO tblcirculation VALUES('$userID', 'reserved','',$libraryNo,'','','$reserve_date','$expire_date', $materialID)";
    $result3 = mysql_query($query3);
    
   // echo $query3;

 
   mysql_close();
    echo"<script>alert('Reservation successful.')</script>";
    echo"<script>history.back()</script>";
  }
 

?>

